
class RpcSignalService {

    // 登录WebRtc信令服务器
    async login(loginReq, timeoutSecond){
        return this.rpcData("signalLogin", loginReq, timeoutSecond?? 60);
    }

    // 创建视频聊天房间
    async createRoom(webRtcCreateRoomReq, timeoutSecond){
        return this.rpcData("roomCreate", webRtcCreateRoomReq, timeoutSecond?? 60);
    }

    // 进入房间
    async joinRoom(webRtcJoinRoomReq, timeoutSecond){
        return this.rpcData("roomJoin", webRtcJoinRoomReq, timeoutSecond?? 60);
    }

    // 退出房间
    async exitRoom(webRtcExitRoomReq, timeoutSecond){
        return this.rpcData("roomExit", webRtcExitRoomReq, timeoutSecond?? 60);
    }

    // 发送sdp
    async sdp(webRtcP2pSdpReq, timeoutSecond){
        return this.rpcData("sdp", webRtcP2pSdpReq, timeoutSecond?? 60);
    }

    // 发送iceCandidate
    async iceCandidate(webRtcP2pCandidateReq, timeoutSecond){
        return this.rpcData("iceCandidate", webRtcP2pCandidateReq, timeoutSecond?? 60);
    }

    async rpcData(path, req, timeOut) {
        let seq = this.createSeq();
        let rpcData = RpcData.build(path, req, seq);
        let resultRpcData = await SignalWebSocket.getInstance().sendData(rpcData, timeOut);
        // let t = this.filterData(resultRpcData.data);
        return resultRpcData.data;
    }

    createSeq(){
        let seq = ImSingleton.getInstance().seqCreator.create();
        seq = seq == null ? "" : seq;
        return seq;
    }

    filterData(obj) {
        if (Objects.isNotNull(obj.errorStatus)){ // errorStatus
            let exceptionResp = obj;
            throw new RpcException(exceptionResp);
        } else {
            return obj;
        }
    }
}